package cs575.wido.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import cs575.wido.action.AddMemberAction;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;

public class InvitationWindow extends JDialog {
	private JComboBox txtUser;
	private Vector<String> userList;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			InvitationWindow dialog = new InvitationWindow();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public InvitationWindow() {
		this.userList = new Vector<String>();
		this.setUserList();
		
		setTitle("Member Invitation");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		
		getContentPane().add(buildFormPanel(), BorderLayout.CENTER);
		getContentPane().add(buildButtonPane(), BorderLayout.SOUTH);
	}
	
	private JComponent buildButtonPane() {
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
	
		JButton okButton = new JButton("OK");
		okButton.setActionCommand("OK");
		okButton.setAction(new AddMemberAction(this));
		
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
	
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				InvitationWindow.this.dispose();
			}
		});
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);
			
		return buttonPane;
	}
	
	private JComponent buildFormPanel() {
		JPanel formPanel = new JPanel();
		
		formPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		formPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		
		JLabel lblGroupId = new JLabel("Group ID");
		formPanel.add(lblGroupId, "1, 2");
		
		JLabel gid = new JLabel("GID###");
		formPanel.add(gid, "3, 2");
		
		JLabel lblUser = new JLabel("User");
		formPanel.add(lblUser, "1, 4, right, default");
		
		txtUser = new SuggestionBox(userList);
		formPanel.add(txtUser, "3, 4, fill, default");
		
		JLabel lblRole = new JLabel("Role");
		formPanel.add(lblRole, "1, 6, right, default");
		
		JComboBox cbRole = new JComboBox();
		formPanel.add(cbRole, "3, 6, fill, default");
		
		JTextArea textArea = new JTextArea();
		JScrollPane scrollPane = new JScrollPane(textArea);
		formPanel.add(scrollPane, "3, 8, fill, fill");
		
		JLabel lblComment = new JLabel("Comment");
		formPanel.add(lblComment, "1, 8");
		
		return formPanel;
	}
	
	public void setUserList() {
		userList.add("Lu");
		userList.add("Kevin");
		userList.add("Zdravko");
		userList.add("LUUUUUU");
		userList.add("ZZZZZZZ");
	}

}
